﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyVoteMVC.Web.Models.Message>" %>

<a id="messagebtn" class="t-button" href="#" style="padding:5px;">发消息</a>
<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery-ui.min.js") %>" type="text/javascript"></script>
<link href="../../Content/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<div id="messageFormContainer" style="display: none;">
    <% using (Ajax.BeginForm("AddMessage",
       "Home",
       null,
       new AjaxOptions() {   OnSuccess = "MessageCallBack" },
       new{id="messageForm"}))
   { %>
    <%: Html.ValidationSummary(true)%>
    <div class="editor-field">
        <%: Html.HiddenFor(model => model.Activity_ID)%>
    </div>
 
    <div class="editor-field">
        <%: Html.TextAreaFor(model => model.Content, new { style="width:400px;height:80px;", onkeydown="show();" ,id="ta"})%>
        <%: Html.ValidationMessageFor(model => model.Content)%>
    </div>
<div id="fontcount"></div>
    <p>
        <input id="messageformsb" type="submit" value="发送"   />
    </p>
    <% } %>
</div>
<script>
    $(function () {
        $('#messageFormContainer').dialog({ title: "发消息", autoOpen: false, modal: true, resizable: false, width: 460, height: 200, position: 'center' });
        $('#messagebtn').click(function () {
            $('#messageFormContainer').dialog('open');
        });
    });

    function MessageCallBack(dd) {
        if (dd) {
            alert(dd.message);
            if (dd.message == "发送成功") {
                RefreshMessageList(dd.ID, dd.name, $('#messageForm textarea[name="Content"]').val(), dd.Date);
            }
        }
        $('#messageformsb').attr("disabled", false);
        $('#messageForm input[name="Title"]').val('');
        $('#messageForm textarea[name="Content"]').val('');

        $('#messageFormContainer').dialog('close');
    }

    $('#messageformsb').click(function () {
        $('#messageformsb').attr("disabled", true);
        $('#messageForm').submit();
    });

    function show() {
        var maxl = 100;
        var ta = $('#ta')[0];
        var s = ta.value.length + 1;
        if (s > maxl)
            ta.value = ta.value
                    .substr(0, maxl - 1)
        else
            $('#fontcount')[0].innerHTML = "已输入：" + s + "/" + maxl
                    + " 字符";
    }

</script>
 
